<?php
/**
 * Created by PhpStorm.
 * User: zhenghao
 * Date: 2015/6/29
 * Time: 10:50
 */

require_once __DIR__."/../bo/NewsBO.php";
require_once __DIR__."/../lib/Page.php";

/**
 * Class NewsServer
 * 网站新闻处理
 */
class NewsServer extends Server {

    private $json_data=array("code" => 200,"msg" => "success");


    //1.新闻专区新闻列表：抓取对应栏目新闻列表
    public function listByType(){

        if(checkEmptyParameter("type_id")) throw new Exception("type_id is null");

        $type_id=$_REQUEST["type_id"];

        //统计分类新闻总数量
        $count=NewsBO::countByType($type_id);

        //获取查询页码
        $page_no=1;
        if(!checkEmptyParameter("page")) $page_no=$_REQUEST["page"];
        //初始化分页处理类
        $page=new Page($count,$page_no);

        //执行查询
        $news=NewsBO::listByType($type_id,$page);

        $this->json_data["news"]=$news;
        $this->json_data["page"]=$page; //传递分页对象

        $this->showJsonData($this->json_data);

    }



    //2.新闻详情
    public function info(){

        if(checkEmptyParameter("id")) throw new Exception("id is null");

        $id=$_REQUEST["id"];

        //获取新闻详情
        $info=NewsBO::info($id);

        $this->json_data["info"]=$info;

        $this->showJsonData($this->json_data);
    }



    //3.获取指定新闻的相关新闻：根据新闻关键字  按最后更新时间倒排，选取5条 (标题、发布时间)
    public function relatedNews(){

        if(checkEmptyParameter("id")) throw new Exception("id is null");

        $id=$_REQUEST["id"];

        $news=NewsBO::relatedNews($id);

        $this->json_data["news"]=$news;

        $this->showJsonData($this->json_data);

    }


    //4.推荐新闻（猜你喜欢）：根据品类cookie 按最后更新时间倒排，选取5条，
    public function loveNews(){

        //获取产品类型编号
        if(checkEmptyParameter("type_ids")) throw new Exception("type_ids is null");

        $type_ids=$_REQUEST["type_ids"];

        //查询新闻
        $news=NewsBO::loveNews($type_ids);

        $this->json_data["news"]=$news;

        $this->showJsonData($this->json_data);

    }



    //5.产品专区新闻列表：抓取对应品目新闻列表
    public function listByProductType(){

        if(checkEmptyParameter("type_id")) throw new Exception("type_id is null");

        $type_id=$_REQUEST["type_id"];

        //统计分类新闻总数量
        $count=NewsBO::countByProductType($type_id);

        //获取查询页码
        $page_no=1;
        if(!checkEmptyParameter("page")) $page_no=$_REQUEST["page"];

        //初始化分页处理类
        $page=new Page($count,$page_no);

        //执行查询
        $news=NewsBO::listByProductType($type_id,$page);

        $this->json_data["news"]=$news;
        $this->json_data["page"]=$page; //传递分页对象

        $this->showJsonData($this->json_data);
    }

}